package com.test.BlockingQueueTest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

/**
 * Created by yang on 17/4/6.
 */
public class StuCEHomework {

    public static String getCEHomework(String token) throws Exception {
        String path = Basic.stuApiRoot + "/collection/homework/get-homework-with-question";
        String source = "iPhoneRCStudent";
        String version = "3.0.1";

        String url = path + "?token=" + token + "&homeworkId=" + Test1.doId + "&version=" + version + "&source=" + source;
        return ConnectionTest.sendGet(url);
    }

    public static void getCEAnswer(String op) throws JSONException {
        if (op != null && !op.isEmpty()){
            JSONObject jsonObject = new JSONObject(op);
            String data = jsonObject.optString("data");
            JSONObject dataJson = new JSONObject(data);
            JSONArray jsonArray = dataJson.optJSONArray("questionList");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject quesJson = jsonArray.getJSONObject(i);
                Basic.questionId.add(quesJson.optString("questionId"));
                Basic.rightAnswer.add(quesJson.optString("rightAnswer"));
                Basic.questionType.add(quesJson.optInt("questionType"));
            }
        }
    }

    public static int putCEAnswer(String token) throws Exception {
        String path = Basic.stuApiRoot + "/collection/homework/put-homework-answer-batch";
        int isAllRight = 0;
        JSONArray jsonArray = new JSONArray();
        int random = Math.random() > 0.8 ? 1 : 0;
        // 全部答对的情况
        if ( random == 1 ){
            isAllRight = 1;
            for (int i = 0; i < Basic.questionId.size(); i++) {
                JSONObject jsonObject = new JSONObject();
                int questionType = Basic.questionType.get(i);
                jsonObject.put("questionId", Basic.questionId.get(i));
                if ( questionType == 4 || questionType == 6 ){
                    jsonObject.put("answer","http://download.cloud.chivox.com:8002/58eca63e73ca25f39805a8ed.mp3");
                    jsonObject.put("type", 1 );
                    jsonObject.put("score", "90");
                    jsonObject.put("analysis", "{\"useref\":1,\"statics\":[{\"char\":\"sil-ay+l\",\"count\":1,\"score\":100},{\"char\":\"ay-l+ay\",\"count\":1,\"score\":100},{\"char\":\"l-ay+k\",\"count\":1,\"score\":100},{\"char\":\"ay-k+ih\",\"count\":1,\"score\":100},{\"char\":\"k-ih+t\",\"count\":1,\"score\":100},{\"char\":\"ih-t+sil\",\"count\":…");
                }else {
                    jsonObject.put("answer", Basic.rightAnswer.get(i));
                    jsonObject.put("type", 0 );

                }
                jsonObject.put("spendTime", "3000");
                jsonArray.put(jsonObject);


            }
        // 有答错情况
        }else {
            for (int i = 0; i < Basic.questionId.size(); i++) {
                int questionType = Basic.questionType.get(i);
                JSONObject jsonObject = new JSONObject();
                int random1 = Math.random() > 0.3 ? 1 : 0;
                // 单个题目答对的情况
                if ( random1 == 1 ) {
                    // 拼朗读、跟读题目的answer
                    if ( questionType == 4 || questionType == 6 ) {
                        jsonObject.put("answer", "http://download.cloud.chivox.com:8002/58eca63e73ca25f39805a8ed.mp3");
                        jsonObject.put("type", 1 );
                        jsonObject.put("score", "90");
                        jsonObject.put("analysis", "{\"useref\":1,\"statics\":[{\"char\":\"sil-ay+l\",\"count\":1,\"score\":100},{\"char\":\"ay-l+ay\",\"count\":1,\"score\":100},{\"char\":\"l-ay+k\",\"count\":1,\"score\":100},{\"char\":\"ay-k+ih\",\"count\":1,\"score\":100},{\"char\":\"k-ih+t\",\"count\":1,\"score\":100},{\"char\":\"ih-t+sil\",\"count\":…");
                    } else {  // 其他类型题目直接拿get-homework-with-question接口返回值中的answer
                        jsonObject.put("answer", Basic.rightAnswer.get(i));
                        jsonObject.put("type", 0 );
                    }
                // 单个题目答错情况
                } else {
                    // 拼不同类型题目的answer
                    if (questionType == 0) {
                        JSONArray originAnswerJsonArray = new JSONArray(Basic.rightAnswer.get(i));   // 将Basic.rightAnswer中的第i个答案转成JSONArray
                        JSONArray fakeJsonArray = new JSONArray();
                        for (int j = 0; j < originAnswerJsonArray.length(); j++) {  // 循环遍历json数组
                            JSONObject jsonObject1 = originAnswerJsonArray.optJSONObject(j);  // 解出单个json
                            int blank_id = jsonObject1.optInt("blank_id");  // 解出"blank_id"对应velue保存
                            JSONObject fakeJson = new JSONObject();
                            fakeJson.put("blank_id", blank_id);
                            fakeJson.put("content", "first");
                            fakeJsonArray.put(fakeJson);
                        }
                        jsonObject.put("answer", fakeJsonArray.toString());
                        jsonObject.put("type", 0);
                    } else if (questionType == 1) {
                        JSONArray originAnswerJsonArray = new JSONArray(Basic.rightAnswer.get(i));
                        JSONArray fakeJsonArray = new JSONArray();
                        for (int j = 0; j < originAnswerJsonArray.length(); j++) {
                            JSONObject jsonObject1 = originAnswerJsonArray.optJSONObject(j);
                            int blank_id = jsonObject1.optInt("blank_id");
                            JSONObject fakeJson = new JSONObject();
                            fakeJson.put("blank_id", blank_id);
                            fakeJson.put("choice", "A");
                            fakeJsonArray.put(fakeJson);
                        }
                        jsonObject.put("answer", fakeJsonArray.toString());
                        jsonObject.put("type", 0);
                    } else if (questionType == 2) {
                        JSONArray originAnswerJsonArray = new JSONArray(Basic.rightAnswer.get(i));
                        JSONArray fakeJsonArray = new JSONArray();
                        for (int j = 0; j < originAnswerJsonArray.length(); j++) {
                            JSONObject jsonObject1 = originAnswerJsonArray.optJSONObject(j);
                            int blank_id = jsonObject1.optInt("blank_id");
                            JSONObject fakeJson = new JSONObject();
                            fakeJson.put("blank_id", blank_id);
                            fakeJson.put("combine", "a");
                            fakeJsonArray.put(fakeJson);
                        }
                        jsonObject.put("answer", fakeJsonArray.toString());
                        jsonObject.put("type", 0);
                    } else if (questionType == 3) {
                        JSONArray answerJsonArray = new JSONArray(Basic.rightAnswer.get(i));
                        JSONArray fakeJsonArray = new JSONArray();
                        for (int j = 0; j < answerJsonArray.length(); j++) {
                            JSONObject jsonObject1 = answerJsonArray.optJSONObject(j);
                            int blank_id = jsonObject1.optInt("blank_id");
                            JSONObject fakeJson = new JSONObject();
                            fakeJson.put("blank_id", blank_id);
                            fakeJson.put("combine", "Touch|your|little|face.");
                            fakeJsonArray.put(fakeJson);
                        }
                        jsonObject.put("answer", fakeJsonArray.toString());
                        jsonObject.put("type", 0);
                    } else if (questionType == 4 || questionType == 6) {
                        jsonObject.put("answer", "http://download.cloud.chivox.com:8002/58eb89269e1fbee688055784.mp3");
                        jsonObject.put("type", 1);
                        jsonObject.put("score", "50");
                        jsonObject.put("analysis", "{\"useref\":1,\"statics\":[{\"char\":\"sil-ay+l\",\"count\":1,\"score\":100},{\"char\":\"ay-l+ay\",\"count\":1,\"score\":100},{\"char\":\"l-ay+k\",\"count\":1,\"score\":100},{\"char\":\"ay-k+ih\",\"count\":1,\"score\":100},{\"char\":\"k-ih+t\",\"count\":1,\"score\":100},{\"char\":\"ih-t+sil\",\"count\":…");
                    } else if (questionType == 5) {
                        // 暂时没有5类型题
                    }

                }
                jsonObject.put("questionId", Basic.questionId.get(i));
                jsonObject.put("spendTime", "3000");
                jsonArray.put(jsonObject);
            }
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("token", token);
        jsonObject.put("homeworkId", Test1.doId);
        jsonObject.put("answerList", jsonArray);
        jsonObject.put("version", "301");

        ConnectionTest.sendPost(path, jsonObject.toString());
        Basic.questionId.clear();
        Basic.rightAnswer.clear();
        Basic.questionType.clear();

        return isAllRight;
    }

    public static String getCEWrong(String token) throws Exception {
        String path = Basic.stuApiRoot + "/collection/homework/get-homework-with-question-only-wrong";
        String url = path + "?homeworkId=" + Test1.doId + "&token=" + token;
        return ConnectionTest.sendGet(url);

    }

    public static void redoCEWrong(String token) throws Exception {
        String path = Basic.stuApiRoot + "/collection/homework/put-homework-redo-answer-batch";
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < Basic.questionId.size(); i++) {
            int random = Math.random() > 0.4 ? 1 : 0;
            int questionType = Basic.questionType.get(i);
            JSONObject jsonObject = new JSONObject();
            // 单个题订正对情况
            if (random == 1) {
                // 拼朗读、跟读题目的answer
                if (questionType == 4 || questionType == 6) {
                    jsonObject.put("answer", "http://download.cloud.chivox.com:8002/58eca63e73ca25f39805a8ed.mp3");
                    jsonObject.put("type", 1);
                    jsonObject.put("score", "90");
                    jsonObject.put("analysis", "{\"useref\":1,\"statics\":[{\"char\":\"sil-ay+l\",\"count\":1,\"score\":100},{\"char\":\"ay-l+ay\",\"count\":1,\"score\":100},{\"char\":\"l-ay+k\",\"count\":1,\"score\":100},{\"char\":\"ay-k+ih\",\"count\":1,\"score\":100},{\"char\":\"k-ih+t\",\"count\":1,\"score\":100},{\"char\":\"ih-t+sil\",\"count\":…");
                } else {  // 其他类型题目直接拿get-homework-with-question接口返回值中的answer
                    jsonObject.put("answer", Basic.rightAnswer.get(i));
                    jsonObject.put("type", 0);
                }
                jsonObject.put("questionId", Basic.questionId.get(i));
                jsonObject.put("spendTime", "3000");
                jsonArray.put(jsonObject);
            }
            // 单个题订正错的情况
            else {
                // 拼不同类型题目的answer
                if (questionType == 0) {
                    JSONArray originAnswerJsonArray = new JSONArray(Basic.rightAnswer.get(i));   // 将Basic.rightAnswer中的第i个答案转成JSONArray
                    JSONArray fakeJsonArray = new JSONArray();
                    for (int j = 0; j < originAnswerJsonArray.length(); j++) {  // 循环遍历json数组
                        JSONObject jsonObject1 = originAnswerJsonArray.optJSONObject(j);  // 解出单个json
                        int blank_id = jsonObject1.optInt("blank_id");  // 解出"blank_id"对应velue保存
                        JSONObject fakeJson = new JSONObject();
                        fakeJson.put("blank_id", blank_id);
                        fakeJson.put("content", "apple");
                        fakeJsonArray.put(fakeJson);
                    }
                    jsonObject.put("answer", fakeJsonArray.toString());
                    jsonObject.put("type", 0);
                } else if (questionType == 1) {
                    JSONArray originAnswerJsonArray = new JSONArray(Basic.rightAnswer.get(i));
                    JSONArray fakeJsonArray = new JSONArray();
                    for (int j = 0; j < originAnswerJsonArray.length(); j++) {
                        JSONObject jsonObject1 = originAnswerJsonArray.optJSONObject(j);
                        int blank_id = jsonObject1.optInt("blank_id");
                        JSONObject fakeJson = new JSONObject();
                        fakeJson.put("blank_id", blank_id);
                        fakeJson.put("choice", "B");
                        fakeJsonArray.put(fakeJson);
                    }
                    jsonObject.put("answer", fakeJsonArray.toString());
                    jsonObject.put("type", 0);
                } else if (questionType == 2) {
                    JSONArray originAnswerJsonArray = new JSONArray(Basic.rightAnswer.get(i));
                    JSONArray fakeJsonArray = new JSONArray();
                    for (int j = 0; j < originAnswerJsonArray.length(); j++) {
                        JSONObject jsonObject1 = originAnswerJsonArray.optJSONObject(j);
                        int blank_id = jsonObject1.optInt("blank_id");
                        JSONObject fakeJson = new JSONObject();
                        fakeJson.put("blank_id", blank_id);
                        fakeJson.put("combine", "c");
                        fakeJsonArray.put(fakeJson);
                    }
                    jsonObject.put("answer", fakeJsonArray.toString());
                    jsonObject.put("type", 0);
                } else if (questionType == 3) {
                    JSONArray answerJsonArray = new JSONArray(Basic.rightAnswer.get(i));
                    JSONArray fakeJsonArray = new JSONArray();
                    for (int j = 0; j < answerJsonArray.length(); j++) {
                        JSONObject jsonObject1 = answerJsonArray.optJSONObject(j);
                        int blank_id = jsonObject1.optInt("blank_id");
                        JSONObject fakeJson = new JSONObject();
                        fakeJson.put("blank_id", blank_id);
                        fakeJson.put("combine", "Touch|your|hand.");
                        fakeJsonArray.put(fakeJson);
                    }
                    jsonObject.put("answer", fakeJsonArray.toString());
                    jsonObject.put("type", 0);
                } else if (questionType == 4 || questionType == 6) {
                    jsonObject.put("answer", "http://download.cloud.chivox.com:8002/58eb89269e1fbee688055784.mp3");
                    jsonObject.put("type", 1);
                    jsonObject.put("score", "50");
                    jsonObject.put("analysis", "{\"useref\":1,\"statics\":[{\"char\":\"sil-ay+l\",\"count\":1,\"score\":100},{\"char\":\"ay-l+ay\",\"count\":1,\"score\":100},{\"char\":\"l-ay+k\",\"count\":1,\"score\":100},{\"char\":\"ay-k+ih\",\"count\":1,\"score\":100},{\"char\":\"k-ih+t\",\"count\":1,\"score\":100},{\"char\":\"ih-t+sil\",\"count\":…");
                } else if (questionType == 5) {
                    // 暂时没有5类型题
                }
                jsonObject.put("questionId", Basic.questionId.get(i));
                jsonObject.put("spendTime", "3000");
                jsonArray.put(jsonObject);
            }

        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("token", token);
        jsonObject.put("homeworkId", Test1.doId);
        jsonObject.put("answerList", jsonArray);
        jsonObject.put("version", "301");

        ConnectionTest.sendPost(path, jsonObject.toString());
        Basic.questionId.clear();
        Basic.rightAnswer.clear();
        Basic.questionType.clear();

    }
    public static void main(String[] args) throws Exception {
        String token = "W/ugdq2LhzHICQTxgZsbFN6x3U5flA1Wnd6+0nH+/wdf+gWH1erL9IypNye1q5oS";
        String question = getCEWrong(token);
        System.out.println(question);
    }

}
